package main;

import main.FactorsOutPut.Output;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.TreeMap;

public class FactorsOutPut extends TreeMap<Integer, Output> {

    public class Paire {

        private int i, j;

        private Paire(int i, int j) {
            this.i = i;
            this.j = j;
        }

        @Override
        public String toString() {
            super.toString();
            return "(" + i + "," + j + ")";
        }
    }

    public class Output extends ArrayList<Paire> {

        private int multip;
        private PrimeFactors primeFactors;

        private Output(Paire paire, PrimeFactors primeFactors, int multip) {
            add(paire);
            this.primeFactors = primeFactors;
            this.multip = multip;
        }

        @Override
        public String toString() {
            String ret = "";
            for (Iterator<Paire> it = this.iterator() ; it.hasNext() ;) {
                ret += it.next();
            }
            return "(" + multip + ")" + primeFactors + "  " + ret + "\n";

        }
    }

    public FactorsOutPut(int entiermax) {
        int p;
        Output output;
        Paire paire;
        for (int i = 2 ; i <= entiermax ; i++) {
            for (int j = i ; j <= entiermax ; j++) {
                p = i * j;
                paire = new Paire(i, j);
                if (!containsKey(p)) {
                    put(p, new Output(paire, new PrimeFactors(p), 1));
                } else {
                    output = get(p);
                    output.multip += 1;
                    output.add(paire);

                }
            }
        }

    }

    public String toText() {
        String retS = "";
        for (Iterator<Integer> it = keySet().iterator() ; it.hasNext() ;) {
            Output output = get(it.next());
            if (output.multip != 1) {
                retS += output.toString();
            }
        }
        return retS;
    }
}
